package xiaoyong;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DrawLine extends JPanel {

	private static final long serialVersionUID = 1L;
	private int r,n;

	public void setR(int r) {
		this.r = r;
	}
	public void setN(int n) {
		this.n = n;
	}
	public static void main(String[] args) {
		
		MyActionEvent myActionEvent = new MyActionEvent();
		myActionEvent.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myActionEvent.setVisible(true);
		
	}
	public void show(DrawLine drawLine){
		JFrame frame = new JFrame();
		frame.getContentPane().add(drawLine);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		double thta;
		
		thta = 2 * (Math.PI) / n;
		PP p[]= new PP[n+1];
		for (int i = 0; i < n; i++) {
			p[i] = new PP();
			p[i].x = (long) (r * (Math.cos(i * thta))+300);
			p[i].y = (long) (r * (Math.sin(i * thta))+300);
		}
		for(int i=0;i<=n-2;i++)
		{
			for(int j=i+1;j<=n-1;j++)
			{
				g.drawLine((int)p[i].x, (int)p[i].y, (int)p[j].x, (int)p[j].y);
			}
		}
	}

}

class PP {
	long x;
	long y;
}
class MyActionEvent extends JFrame implements ActionListener{

	JButton ok = new JButton("ok");
	JTextField radius = new JTextField(15);
	JTextField points = new JTextField(15);
	JLabel radiusTag = new JLabel("输入圆的半径:");
	JLabel pointsTag = new JLabel("输入等分点数:");
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JPanel p3 = new JPanel();
	JPanel p4 = new JPanel();
	
	MyActionEvent(){
		
		this.setSize(600,600);
		Container container = this.getContentPane();
		FlowLayout fleft = new FlowLayout(FlowLayout.CENTER,10,10);
		FlowLayout fright = new FlowLayout(FlowLayout.RIGHT,10,10);
		BorderLayout border = new BorderLayout(10,10);
		GridLayout g = new GridLayout(2,1);
		container.setLayout(border);
		p1.setLayout(fleft);
		p2.setLayout(fleft);
		p3.setLayout(fright);
		p4.setLayout(g);
		p1.add(pointsTag);
		p1.add(points);
		p2.add(radiusTag);
		p2.add(radius);
		p3.add(ok);
		p4.add(p1);
		p4.add(p2);
		container.add(p4,BorderLayout.CENTER);
		container.add(p3,BorderLayout.SOUTH);
		this.setTitle("输入参数");
		ok.addActionListener(this);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == ok){
			int n = Integer.parseInt(points.getText());
			int r = Integer.parseInt(radius.getText());
			if(r >= 3&& n > 0){
				DrawLine drawLine = new DrawLine();
				drawLine.setR(r);
				drawLine.setN(n);
				drawLine.show(drawLine);
			}
		}
	}
	
}